package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@DubboService
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member insertOrUpdate(Member member) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(member);
        //根据手机号查询出预约人
        Member selectOne = memberMapper.selectOneByExample(example);
        if (null != selectOne) { //如果不为空 代表该预约人已经与预约过了 进行更改
            member.setId(selectOne.getId());
            //根据key更新数据库数据
            memberMapper.updateByPrimaryKeySelective(member);
        } else {
            //到这里证明预约人不存在 新增预约人
            member.setRegTime(new Date());
            memberMapper.insertSelective(member);
        }
        return member;
    }

    @Override
    public Member findById(Integer memberId) {
        return memberMapper.selectByPrimaryKey(memberId);
    }

    @Override
    public List<Integer> countByMonth(List<String> months) {
        //创建返回值集合
        List<Integer> resultCount = new ArrayList<>();
        //遍历月份
        for (String month : months) {
            //设置每月31号
            String endDate = month+".31";
            //创建条件
            Example example = new Example(Member.class);
            Example.Criteria criteria = example.createCriteria();
            //将日期传入条件中
            criteria.andLessThanOrEqualTo("regTime",endDate);
            //统计小于等于每月31号的用户数量
            int count = memberMapper.selectCountByExample(example);
            resultCount.add(count);
        }
        return resultCount;
    }
}
